//
//  DeviceInfoViewController.m
//  Rail
//
//  Created by hychen on 2018/5/13.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "DeviceInfoViewController.h"
#import "LineDetailDescTableViewCell.h"
#import "ButtonTableViewCell.h"
#import "GetDeviceViewController.h"

@interface DeviceInfoViewController ()
@property (nonatomic, strong) UIButton *collectionButton;
@property (nonatomic, assign) BOOL isLoading;
@property (nonatomic, strong) DeviceUserdDetailModel *detailModel;
@end

@implementation DeviceInfoViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:@"设备信息"];
    self.tableView.mj_header = nil;
//    self.collectionButton = [self.navigationItem addRightNavItem:self normalImage:[UIImage imageNamed:@"app_title_bar_back_n_ic"] highlightedImage:[UIImage imageNamed:@"app_title_bar_back_p_ic"] selector:@selector(clickCollectionButton:)];
//    [self.collectionButton setImage:[UIImage imageNamed:@"app_title_bar_back_n_ic"] forState:UIControlStateNormal];
//    [self.collectionButton setImage:[UIImage imageNamed:@"app_title_bar_back_p_ic"] forState:UIControlStateSelected];
    if ([self.model.typecode isEqualToString:@"GPS"] || [self.model.typecode isEqualToString:@"VOIP-A"]) {
        
    }
    else {
        [self reloadData];
    }
    
}

- (void)reloadData {
    self.dataArray = @[
                       @{@"title":@"设备名称",@"content":self.model.device_name?:@""},
                       @{@"title":@"设备类型",@"content":self.model.typeName?:@""},
                       @{@"title":@"设备状态",@"content":[WarnLevelConfig deviceStatusString:self.model.status]?:@""},
                       @{@"title":@"操作人员",@"content":self.model.op_name?:@""},
                       @{@"title":@"操作时间",@"content":[BMUtils chineseYYYYMMDDHHmmWithServiceTime:self.model.op_time]?:@""},
                       ].mutableCopy;
    
    if ([self.model.typecode isEqualToString:@"GPS"] || [self.model.typecode isEqualToString:@"VOIP-A"]) {
        
        NSMutableArray *btnArray = [NSMutableArray array];
        if (!self.detailModel) {
            //领用设备
            [btnArray addObject:@{@"type":@"button",@"title":@"领用设备"}];
        }
        else {
            //归还设备
            [self.dataArray addObjectsFromArray:@[
                                                  @{@"title":@"领用人员",@"content":self.detailModel.username?:@""},
                                                  @{@"title":@"领用时间",@"content":self.detailModel.reg_time?:@""},
                                                  ]];
            [btnArray addObject:@{@"type":@"button",@"title":@"归还设备"}];
        }
        [self.dataArray addObjectsFromArray:btnArray];
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void)clickCollectionButton:(UIButton *)button {
//    BOOL isFav = button.selected;
//    if (isFav) {
//        [self cancelFactoryCollection];
//    }
//    else {
//        [self addFactoryCollection];
//    }
//}
//- (void)addFactoryCollection {
//    if (self.isLoading) {
//        return;
//    }
//    if (self.model.is_fav) {
//        return;
//    }
//    self.isLoading = YES;
//    Show_Msg(@"收藏中")
//    BMWeakSelf
////    [HttpRequest addFavWithLineId:@"" areaId:self.model.favid deviceId:@"" success:^(id object) {
////        weakSelf.collectionButton.selected = YES;
////        Show_Msg_Success(@"收藏成功")
////        weakSelf.isLoading = NO;
////    } failure:^(NSString *code, NSString *msg) {
////        Show_Msg_Error(msg)
////        weakSelf.isLoading = NO;
////    }];
//}
//
//- (void)cancelFactoryCollection {
//    if (self.isLoading) {
//        return;
//    }
//    self.isLoading = YES;
//    Show_Msg(@"取消收藏中")
//    BMWeakSelf
////    [HttpRequest cancelFavWithFavId:self.areaid?:@"" success:^(id object) {
////        weakSelf.collectionButton.selected = NO;
////        Show_Msg_Success(@"取消收藏成功")
////        weakSelf.isLoading = NO;
////    } failure:^(NSString *code, NSString *msg) {
////        Show_Msg_Error(msg)
////        weakSelf.isLoading = NO;
////    }];
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.row];
    BMWeakSelf
    if ([[dict objectForKeySafe:@"type"] isEqualToString:@"button"]) {
        ButtonTableViewCell *cell = [ButtonTableViewCell cellWithTableView:tableView class:[ButtonTableViewCell class]];
        cell.buttonTitle = [dict objectForKeySafe:@"title"];
        [cell setBlockClickEventButton:^{
            [weakSelf processDevice];
        }];
        return cell;
    }
    LineDetailDescTableViewCell *cell = [LineDetailDescTableViewCell cellWithTableView:tableView class:[LineDetailDescTableViewCell class]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.showArrow = NO;
    cell.titleLabel.text = [dict objectForKey:@"title"];
    cell.contentLabel.text = [dict objectForKey:@"content"];
    if (indexPath.row == 2 && self.model.status == DeviceStatusOnline) {
        cell.contentLabel.textColor = Color_RGB(0, 193, 224);
    }
    else {
        cell.contentLabel.textColor = Color_RGB(86, 86, 86);
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [self.dataArray objectAtIndex:indexPath.row];
    if ([[dict objectForKeySafe:@"type"] isEqualToString:@"button"]) {
        return 150.f;
    }
    return 50.f;
}

- (void)processDevice {
    if (!self.detailModel) {
        //领用设备
        [self requestRentData];
    }
    else {
        //归还设备
        [self requestRevertData];
    }
}

///领用设备
- (void)requestRentData {
    BMWeakSelf
    GetDeviceViewController *vc = [GetDeviceViewController new];
    vc.model = self.model;
    [vc setBlockGetDeviceSuccess:^(NSString *regName) {
        [weakSelf rentSuccessWithName:regName];
    }];
    [self.navigationController pushViewController:vc animated:YES];
    
//    BMWeakSelf
//    if ([self.model.typecode isEqualToString:@"GPS"]) {
//        [HttpRequest getLocateDeviceWithDeviceId:self.model.deviceid?:@"" UserType:@"0" userAccount:[GlobalManage sharedManage].userInfoModel.account mobile:[GlobalManage sharedManage].userInfoModel.mobile username:[GlobalManage sharedManage].userInfoModel.username company:@"" success:^(id object) {
//            [weakSelf rentSuccess];
//        } failure:^(NSString *code, NSString *msg) {
//            Show_SV_Error_Msg(msg?:@"领用失败")
//        }];
//    }
//    else if ([self.model.typecode isEqualToString:@"VOIP-A"]) {
//        [HttpRequest getTbkDeviceWithAccount:[GlobalManage sharedManage].userInfoModel.account userType:@"0" mobile:[GlobalManage sharedManage].userInfoModel.mobile userName:[GlobalManage sharedManage].userInfoModel.username company:@"" deviceId:self.model.deviceid success:^(id object) {
//            [weakSelf rentSuccess];
//        } failure:^(NSString *code, NSString *msg) {
//            Show_SV_Error_Msg(msg?:@"领用失败")
//        }];
//    }
}

- (void)rentSuccessWithName:(NSString *)regName {
    //领用设备状态改为已领用
    DeviceUserdDetailModel *obj = [DeviceUserdDetailModel new];
    obj.reg_time = [NSString stringWithFormat:@"%@",[[NSDate date] formatYearMonthDayHourMinuteChinese_lib]];
    obj.username = regName?:@"";
    self.detailModel = obj;
    //领用人员领用时间赋值
    [self reloadData];
}

///归还设备
- (void)requestRevertData {
    BMWeakSelf
    Show_SV_NoMsg
    if ([self.model.typecode isEqualToString:@"GPS"]) {
        [HttpRequest returnLocateDeviceWithDeviceId:self.model.deviceid success:^(id object) {
            Show_Msg_Success(@"归还成功")
            [weakSelf returnSuccess];
        } failure:^(NSString *code, NSString *msg) {
            Show_SV_Error_Msg(msg?:@"归还失败")
        }];
    }
    else if ([self.model.typecode isEqualToString:@"VOIP-A"]) {
        [HttpRequest returnTbkDeviceWithDeviceId:self.model.deviceid account:[GlobalManage sharedManage].userInfoModel.account success:^(id object) {
            Show_Msg_Success(@"归还成功")
            [weakSelf returnSuccess];
        } failure:^(NSString *code, NSString *msg) {
            Show_SV_Error_Msg(msg?:@"归还失败")
        }];
    }
}

- (void)returnSuccess {
    //领用设备状态改为未领用
    self.detailModel = nil;
    [self reloadData];
}

- (void)setModel:(DeviceStatisticModel *)model {
    _model = model;
    [self requestDeviceDetail];
}

#pragma mark -
- (void)requestDeviceDetail {
    [self requestLocatedDetail];
//    if ([self.model.typecode isEqualToString:@"VOIP-A"]) {
//        [self requestLocatedDetail];
//    }
//    else if ([self.model.typecode isEqualToString:@"GPS"]) {
//        [self requestTalkDetail];
//    }
}

- (void)requestLocatedDetail {
    BMWeakSelf
    [HttpRequest loadDeviceUsedDetailDataWithDeviceId:self.model.deviceid?:@"" success:^(DeviceUserdDetailModel *object) {
        weakSelf.detailModel = object;
        [weakSelf reloadData];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf reloadData];
    }];
}

- (void)requestTalkDetail {
    BMWeakSelf
    [HttpRequest getTbkDeviceDetailWithDeviceId:self.model.deviceid?:@"" success:^(DeviceUserdDetailModel *object) {
        weakSelf.detailModel = object;
        [weakSelf reloadData];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf reloadData];
    }];
}
@end
